package com.yaiys.jvm;

import sun.misc.Unsafe;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicAbaProblemTest {

    static AtomicInteger atomicInteger = new AtomicInteger();

    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = atomicInteger.get();
                System.out.println("操作线程初始值" + Thread.currentThread().getName() + "------" + atomicInteger.get());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                boolean b = atomicInteger.compareAndSet(1, 2);
                if (b) {
                    System.out.println("操作线程初始值" + b + "------" + atomicInteger.get());

                } else {
                    System.out.println("修改失败");
                }
            }

        }, "主线程");

        Thread other = new Thread(new Runnable() {
            @Override
            public void run() {
                atomicInteger.incrementAndGet();
                System.out.println("other" + Thread.currentThread().getName() + "------" + atomicInteger.get());

                atomicInteger.decrementAndGet();
                System.out.println("other" + Thread.currentThread().getName() + "------" + atomicInteger.get());

            }

        }, "主线程");
        thread.start();
        other.start();
        System.out.println("操作线程修改后" + Thread.currentThread().getName() + "------" + atomicInteger.get());
        Unsafe unsafe=Unsafe.getUnsafe();
    }
}
